---
title: "Schäfer_BJPS_Data_preparation_ESS 1-7"
author: "Armin Schäfer, WWU Münster"
date: |
  `r lubridate::today()`
output: html_document
---

This R Markdown (Rmd) file contains material to reproduce the results reported in Schäfer "Cultural Backlash? How (not) to explain the rise of authoritarian populism." In this file, I prepare **European Social Survey data (waves 1 to 7)** for further analyses:

*European Social Survey (2018). ESS 1-9, European Social Survey Cumulative File, Study Description. Bergen: NSD---Norwegian Centre for Research Data for ESS ERIC. [doi:10.21338/NSD-ESS-CUMULATIVE](http://dx.doi.org/10.21338/NSD-ESS-CUMULATIVE)*.

The results reported in the paper can be reproduced with the file "replication_figures_tables.Rmd." Note, however, as this script always loads the newest version of the ESS data, [updates](https://www.europeansocialsurvey.org/data/alerts.html) might lead to slight changes in the results and therefore might differ slightly from the published version.

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

# useful function
tabl <- function(...) table(..., useNA="ifany")

```

*Note*: I rely on the [R package management tool `pacman`](). Please run `install.packages("pacman")` to install the package from CRAN. The command `pacman::p_load()` will then import all specified packages and install them first in case they are currently not installed on your machine.

```{r load libraries}

# Loading R-libraries.

if (!require("pacman")) install.packages("pacman")
pacman::p_load(essurvey, readr, tidyverse,
               dplyr, psych, naniar, here, 
               srvyr, questionr, labelled, scales)


```

# Load the data ----

Load the dataset and prepare for analyses. To access ESS data, you have to register first. Go to the [ESS website](http://www.europeansocialsurvey.org/user/new) and provide your details. 

```{r load data, results="hide"}

essurvey::set_email("your email")

# download ESS data
ess_1_9 <- import_all_rounds()

ess_1 <- ess_1_9[[1]]
ess_1 <- ess_1 %>%
  dplyr::select(!prtvde1) %>%
  unite("ess_party_id", prtvtat:prtvtsi, na.rm=T, remove=FALSE) %>%
  mutate(trstprt = NA,
         euftf = NA)

ess_2 <- ess_1_9[[2]]
ess_2 <- ess_2 %>%
  dplyr::select(!prtvade1) %>%
  unite("ess_party_id", prtvtat:prtvtua, na.rm=T, remove=FALSE)

ess_3 <- ess_1_9[[3]]
ess_3 <- ess_3 %>%
  dplyr::select(!prtvbde1)%>%
  unite("ess_party_id", prtvtaat:prtvtaua, na.rm=T, remove=FALSE) 

ess_4 <- ess_1_9[[4]]
ess_4 <- ess_4 %>%
 dplyr::select(!prtvbde1) %>%
  unite("ess_party_id", prtvtaat:prtvtbua, na.rm=T, remove=FALSE)

ess_5 <- ess_1_9[[5]]
ess_5 <- ess_5 %>%
  dplyr::select(!c(prtvcde1,prtvlt1,prtvlt2)) %>%
  unite("ess_party_id", prtvtcbe:prtvtbua, na.rm=T, remove=FALSE) %>%
  mutate(edulvla = case_when(
    edulvlb==113 | edulvlb==129 ~1,
    edulvlb>199 & edulvlb<300 ~2,
    edulvlb>299 & edulvlb<400 ~3,
    edulvlb>399 & edulvlb<500 ~4,
    edulvlb>499 & edulvlb<801 ~5,
  ),
  euftf = NA)

ess_6 <- ess_1_9[[6]]
ess_6 <- ess_6 %>%
  dplyr::select(!c(prtvdde1,prtvalt1,prtvalt2)) %>%
  unite("ess_party_id", prtvtal:prtvtxk, na.rm=T, remove=FALSE) %>%
  mutate(edulvla = case_when(
    edulvlb==113 | edulvlb==129 ~1,
    edulvlb>199 & edulvlb<300 ~2,
    edulvlb>299 & edulvlb<400 ~3,
    edulvlb>399 & edulvlb<500 ~4,
    edulvlb>499 & edulvlb<801 ~5,
  ))

ess_7 <- ess_1_9[[7]]
ess_7 <- ess_7 %>%
  dplyr::select(!c(prtvede1, prtvalt1, prtvalt2)) %>%
  unite("ess_party_id", prtvtbat:prtvtesi, na.rm=T, remove=FALSE) %>%
  mutate(edulvla = case_when(
    edulvlb==113 | edulvlb==129 ~1,
    edulvlb>199 & edulvlb<300 ~2,
    edulvlb>299 & edulvlb<400 ~3,
    edulvlb>399 & edulvlb<500 ~4,
    edulvlb>499 & edulvlb<801 ~5
  ))

```

# Oesch scheme 1-5 ----

To be able to include social class in the analyses, I rely on the [scripts](https://people.unil.ch/danieloesch/scripts/) Daniel Oesch and Amal Tawfik have created and made publicly available. The coding uses ISCO codes of respondents and their partners to create occupational groupings. As there are small differences between different waves, the code has to be run separately for waves 1 to 5 and 6 to 7.

Add Oesch class scheme for waves 1-5
```{r oesch_1_5}

common_cols_1_5 <- map(mget(paste0("ess_", 1:5)), names) %>%
       reduce(intersect)

d <- rbind(subset(ess_1, select = common_cols_1_5),
             subset(ess_2, select = common_cols_1_5),
             subset(ess_3, select = common_cols_1_5),
             subset(ess_4, select = common_cols_1_5),
             subset(ess_5, select = common_cols_1_5)) %>%
  mutate(ess_party_id = as.numeric(ess_party_id))

d$isco_mainjob <- d$iscoco
d$isco_mainjob[is.na(d$isco_mainjob)] <- -9


#### Recode employment status for respondents
d$emplrel_r <- d$emplrel
d$emplrel_r[is.na(d$emplrel_r)] <- 9
d$emplrel_r[d$cntry == "FR" & (d$essround == 1 | d$essround == 2)] <- -9 # Replace this command line with [SYNTAX A] or [SYNTAX C]
d$emplrel_r[d$cntry == "HU" & d$essround == 2] <- -9 # Replace this command line with [SYNTAX E]

d$emplno_r <- d$emplno
d$emplno_r[is.na(d$emplno_r)] <- 0
d$emplno_r[d$emplno_r >= 1 & d$emplno_r <= 9] <- 1
d$emplno_r[d$emplno_r >= 10 & d$emplno_r <= 66665] <- 2

d$selfem_mainjob <- NA
d$selfem_mainjob[d$emplrel_r == 1 | d$emplrel_r == 9] <- 1
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 0] <- 2
d$selfem_mainjob[d$emplrel_r == 3] <- 2
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 1] <- 3
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 2] <- 4

#################################################
# Create Oesch class schema for respondents
#################################################

d$class16_r <- -9

# Large employers (1)

d$class16_r[d$selfem_mainjob == 4] <- 1

# Self-employed professionals (2)

d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2000 & d$isco_mainjob <= 2229] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2300 & d$isco_mainjob <= 2470] <- 2

# Small business owners with employees (3)

d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 1000 & d$isco_mainjob <= 1999] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 3000 & d$isco_mainjob <= 9333] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2230] <- 3

# Small business owners without employees (4)

d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 1000 & d$isco_mainjob <= 1999] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 3000 & d$isco_mainjob <= 9333] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2230] <- 4

# Technical experts (5)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2100 &  d$isco_mainjob <= 2213] <- 5

# Technicians (6)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3100 &  d$isco_mainjob <= 3152] <- 6
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3210 &  d$isco_mainjob <= 3213] <- 6
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3434] <- 6

# Skilled manual (7)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 6000 &  d$isco_mainjob <= 7442] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8310 &  d$isco_mainjob <= 8312] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8324 &  d$isco_mainjob <= 8330] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8332 &  d$isco_mainjob <= 8340] <- 7

# Low-skilled manual (8)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8000 &  d$isco_mainjob <= 8300] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8320 &  d$isco_mainjob <= 8321] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8331] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9153 &  d$isco_mainjob <= 9333] <- 8

# Higher-grade managers and administrators (9)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1000 &  d$isco_mainjob <= 1239] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2400 &  d$isco_mainjob <= 2429] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2441] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2470] <- 9

# Lower-grade managers and administrators (10)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1300 &  d$isco_mainjob <= 1319] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3400 &  d$isco_mainjob <= 3433] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3440 &  d$isco_mainjob <= 3450] <- 10

# Skilled clerks (11)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4000 &  d$isco_mainjob <= 4112] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4114 &  d$isco_mainjob <= 4210] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4212 &  d$isco_mainjob <= 4222] <- 11

# Unskilled clerks (12)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4113] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4211] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4223] <- 12

# Socio-cultural professionals (13)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2220 &  d$isco_mainjob <= 2229] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2300 &  d$isco_mainjob <= 2320] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2340 &  d$isco_mainjob <= 2359] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2430 &  d$isco_mainjob <= 2440] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2442 &  d$isco_mainjob <= 2443] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2445] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2451] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2460] <- 13

# Socio-cultural semi-professionals (14)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2230] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2330 &  d$isco_mainjob <= 2332] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2444] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2446 &  d$isco_mainjob <= 2450] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2452 &  d$isco_mainjob <= 2455] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3200] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3220 &  d$isco_mainjob <= 3224] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3226] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3229 &  d$isco_mainjob <= 3340] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3460 &  d$isco_mainjob <= 3472] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3480] <- 14

# Skilled service (15)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3225] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3227 &  d$isco_mainjob <= 3228] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3473 &  d$isco_mainjob <= 3475] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5000 &  d$isco_mainjob <= 5113] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5122] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5131 &  d$isco_mainjob <= 5132] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5140 &  d$isco_mainjob <= 5141] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5143] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5160 &  d$isco_mainjob <= 5220] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8323] <- 15

# Low-skilled service (16)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5120 &  d$isco_mainjob <= 5121] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5123 &  d$isco_mainjob <= 5130] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5133 &  d$isco_mainjob <= 5139] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5142] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5149] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5230] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8322] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9100 &  d$isco_mainjob <= 9152] <- 16


d$class16_r[d$class16_r == -9] <- NA


d$class8_r <- NA
d$class8_r[d$class16_r <= 2] <- 1
d$class8_r[d$class16_r == 3 | d$class16_r == 4] <- 2
d$class8_r[d$class16_r == 5 | d$class16_r == 6] <- 3
d$class8_r[d$class16_r == 7 | d$class16_r == 8] <- 4
d$class8_r[d$class16_r == 9 | d$class16_r == 10] <- 5
d$class8_r[d$class16_r == 11 | d$class16_r == 12] <- 6
d$class8_r[d$class16_r == 13 | d$class16_r == 14] <- 7
d$class8_r[d$class16_r == 15 | d$class16_r == 16] <- 8

d$class5_r <- NA
d$class5_r[d$class16_r <= 2 | d$class16_r == 5 | d$class16_r == 9 | d$class16_r == 13] <- 1
d$class5_r[d$class16_r == 6 | d$class16_r == 10 | d$class16_r == 14] <- 2
d$class5_r[d$class16_r == 3 | d$class16_r == 4] <- 3
d$class5_r[d$class16_r == 7 | d$class16_r == 11 | d$class16_r == 15] <- 4
d$class5_r[d$class16_r == 8 | d$class16_r == 12 | d$class16_r == 16] <- 5

#######################################################################################
# Partner's Oesch class position
# Recode and create variables used to construct class variable for partners
# Variables used to construct class variable for partners: iscocop, emprelp, emplnop
#######################################################################################

#### Recode occupation variable (isco88 com 4-digit) for partners

d$isco_partner <- d$iscocop
d$isco_partner[is.na(d$isco_partner)] <- -9


#### Recode employment status for partners
d$emplrel_p <- d$emprelp
d$emplrel_p[is.na(d$emplrel_p)] <- 9
d$emplrel_p[d$cntry == "FR" & (d$essround == 1 | d$essround == 2)] <- -9 # Replace this command line with [SYNTAX B] or [SYNTAX D]
d$emplrel_p[d$cntry == "HU" & d$essround == 2] <- -9 # Replace this command line with [SYNTAX F]

d$emplno_p <- d$emplnop
d$emplno_p[is.na(d$emplno_p)] <- 0
d$emplno_p[d$emplno_p >= 1 & d$emplno_p <= 9] <- 1
d$emplno_p[d$emplno_p >= 10 & d$emplno_p <= 66665] <- 2


d$selfem_partner <- NA
d$selfem_partner[d$emplrel_p == 1 | d$emplrel_p == 9] <- 1
d$selfem_partner[d$emplrel_p == 2 & d$emplno_p == 0] <- 2
d$selfem_partner[d$emplrel_p == 3] <- 2
d$selfem_partner[d$emplrel_p == 2 & d$emplno_p == 1] <- 3
d$selfem_partner[d$emplrel_p == 2 & d$emplno_p == 2] <- 4

############################################
# Create Oesch class schema for partners
############################################

d$class16_p <- -9

# Large employers (1)

d$class16_p[d$selfem_partner == 4] <- 1

# Self-employed professionals (2)

d$class16_p[(d$selfem_partner == 2 | d$selfem_partner == 3) & d$isco_partner >= 2000 & d$isco_partner <= 2229] <- 2
d$class16_p[(d$selfem_partner == 2 | d$selfem_partner == 3) & d$isco_partner >= 2300 & d$isco_partner <= 2470] <- 2

# Small business owners with employees (3)

d$class16_p[d$selfem_partner == 3 & d$isco_partner >= 1000 & d$isco_partner <= 1999] <- 3
d$class16_p[d$selfem_partner == 3 & d$isco_partner >= 3000 & d$isco_partner <= 9333] <- 3
d$class16_p[d$selfem_partner == 3 & d$isco_partner == 2230] <- 3

# Small business owners without employees (4)

d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 1000 & d$isco_partner <= 1999] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 3000 & d$isco_partner <= 9333] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2230] <- 4

# Technical experts (5)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2100 & d$isco_partner <= 2213] <- 5

# Technicians (6)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3100 & d$isco_partner <= 3152] <- 6
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3210 & d$isco_partner <= 3213] <- 6
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3434] <- 6

# Skilled manual (7)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 6000 & d$isco_partner <= 7442] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8310 & d$isco_partner <= 8312] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8324 & d$isco_partner <= 8330] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8332 & d$isco_partner <= 8340] <- 7

# Low-skilled manual (8)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8000 & d$isco_partner <= 8300] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8320 & d$isco_partner <= 8321] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8331] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9153 & d$isco_partner <= 9333] <- 8

# Higher-grade managers and administrators (9)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1000 & d$isco_partner <= 1239] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2400 & d$isco_partner <= 2429] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2441] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2470] <- 9

# Lower-grade managers and administrators (10)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1300 & d$isco_partner <= 1319] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3400 & d$isco_partner <= 3433] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3440 & d$isco_partner <= 3450] <- 10

# Skilled clerks (11)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4000 & d$isco_partner <= 4112] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4114 & d$isco_partner <= 4210] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4212 & d$isco_partner <= 4222] <- 11

# Unskilled clerks (12)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4113] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4211] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4223] <- 12

# Socio-cultural professionals (13)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2220 & d$isco_partner <= 2229] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2300 & d$isco_partner <= 2320] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2340 & d$isco_partner <= 2359] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2430 & d$isco_partner <= 2440] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2442 & d$isco_partner <= 2443] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2445] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2451] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2460] <- 13

# Socio-cultural semi-professionals (14)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2230] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2330 & d$isco_partner <= 2332] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2444] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2446 & d$isco_partner <= 2450] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2452 & d$isco_partner <= 2455] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3200] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3220 & d$isco_partner <= 3224] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3226] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3229 & d$isco_partner <= 3340] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3460 & d$isco_partner <= 3472] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3480] <- 14

# Skilled service (15)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3225] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3227 & d$isco_partner <= 3228] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3473 & d$isco_partner <= 3475] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5000 & d$isco_partner <= 5113] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5122] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5131 & d$isco_partner <= 5132] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5140 & d$isco_partner <= 5141] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5143] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5160 & d$isco_partner <= 5220] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8323] <- 15

# Low-skilled service (16)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5120 & d$isco_partner <= 5121] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5123 & d$isco_partner <= 5130] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5133 & d$isco_partner <= 5139] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5142] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5149] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5230] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8322] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9100 & d$isco_partner <= 9152] <- 16


d$class16_p[d$class16_p == -9] <- NA


d$class8_p <- NA
d$class8_p[d$class16_p <= 2] <- 1
d$class8_p[d$class16_p == 3 | d$class16_p == 4] <- 2
d$class8_p[d$class16_p == 5 | d$class16_p == 6] <- 3
d$class8_p[d$class16_p == 7 | d$class16_p == 8] <- 4
d$class8_p[d$class16_p == 9 | d$class16_p == 10] <- 5
d$class8_p[d$class16_p == 11 | d$class16_p == 12] <- 6
d$class8_p[d$class16_p == 13 | d$class16_p == 14] <- 7
d$class8_p[d$class16_p == 15 | d$class16_p == 16] <- 8

d$class5_p <- NA
d$class5_p[d$class16_p <= 2 | d$class16_p == 5 | d$class16_p == 9 | d$class16_p == 13] <- 1
d$class5_p[d$class16_p == 6 | d$class16_p == 10 | d$class16_p == 14] <- 2
d$class5_p[d$class16_p == 3 | d$class16_p == 4] <- 3
d$class5_p[d$class16_p == 7 | d$class16_p == 11 | d$class16_p == 15] <- 4
d$class5_p[d$class16_p == 8 | d$class16_p == 12 | d$class16_p == 16] <- 5


####################################################################################################
# Final Oesch class position
# Merge two class variables (respondents and partners)
# Assign the partner's Oesch class position when the respondent's Oesch class position is missing:
####################################################################################################

d$class16 <- ifelse(!is.na(d$class16_r), d$class16_r, d$class16_p)

d$class8 <- NA
d$class8[d$class16 <= 2] <- 1
d$class8[d$class16 == 3 | d$class16 == 4] <- 2
d$class8[d$class16 == 5 | d$class16 == 6] <- 3
d$class8[d$class16 == 7 | d$class16 == 8] <- 4
d$class8[d$class16 == 9 | d$class16 == 10] <- 5
d$class8[d$class16 == 11 | d$class16 == 12] <- 6
d$class8[d$class16 == 13 | d$class16 == 14] <- 7
d$class8[d$class16 == 15 | d$class16 == 16] <- 8

d$class5 <- NA
d$class5[d$class16 <= 2 | d$class16 == 5 | d$class16 == 9 | d$class16 == 13] <- 1
d$class5[d$class16 == 6 | d$class16 == 10 | d$class16 == 14] <- 2
d$class5[d$class16 == 3 | d$class16 == 4] <- 3
d$class5[d$class16 == 7 | d$class16 == 11 | d$class16 == 15] <- 4
d$class5[d$class16 == 8 | d$class16 == 12 | d$class16 == 16] <- 5

ess_1_5 <- d

ess_1_5$euftf <- as.double(ess_1_5$euftf)


```

# Oesch scheme 6-7----

Add Oesch class scheme for waves 6-7.
```{r oesch_6_7}

common_cols_6_7 <- map(mget(paste0("ess_", 6:7)), names) %>%
       reduce(intersect)

d <- rbind(subset(ess_6, select = common_cols_6_7),
             subset(ess_7, select = common_cols_6_7)) %>%
  mutate(ess_party_id = as.numeric(ess_party_id),
         euftf = as.character(euftf))

d$isco_mainjob <- d$isco08
d$isco_mainjob[is.na(d$isco_mainjob)] <- -9


#### Recode employment status for respondents

d$emplrel_r <- d$emplrel
d$emplrel_r[is.na(d$emplrel_r)] <- 9


d$emplno_r <- d$emplno
d$emplno_r[is.na(d$emplno_r)] <- 0
d$emplno_r[d$emplno_r >= 1 & d$emplno_r <= 9] <- 1
d$emplno_r[d$emplno_r >= 10 & d$emplno_r <= 66665] <- 2

d$selfem_mainjob <- NA
d$selfem_mainjob[d$emplrel_r == 1 | d$emplrel_r == 9] <- 1
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 0] <- 2
d$selfem_mainjob[d$emplrel_r == 3] <- 2
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 1] <- 3
d$selfem_mainjob[d$emplrel_r == 2 & d$emplno_r == 2] <- 4


#################################################
# Create Oesch class schema for respondents
#################################################

d$class16_r <- -9

# Large employers (1)

d$class16_r[d$selfem_mainjob == 4] <- 1

# Self-employed professionals (2)

d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2000 & d$isco_mainjob <= 2162] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2164 & d$isco_mainjob <= 2165] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2200 & d$isco_mainjob <= 2212] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob == 2250] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2261 & d$isco_mainjob <= 2262] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2300 & d$isco_mainjob <= 2330] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2350 & d$isco_mainjob <= 2352] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2359 & d$isco_mainjob <= 2432] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2500 & d$isco_mainjob <= 2619] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob == 2621] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2630 & d$isco_mainjob <= 2634] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2636 & d$isco_mainjob <= 2640] <- 2
d$class16_r[(d$selfem_mainjob == 2 | d$selfem_mainjob == 3) & d$isco_mainjob >= 2642 & d$isco_mainjob <= 2643] <- 2

# Small business owners with employees (3)

d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 1000 & d$isco_mainjob <= 1439] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2163] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2166] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2220 & d$isco_mainjob <= 2240] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2260] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2263 & d$isco_mainjob <= 2269] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2340 & d$isco_mainjob <= 2342] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2353 & d$isco_mainjob <= 2356] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2433 & d$isco_mainjob <= 2434] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2620] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2622] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2635] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob == 2641] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 2650 & d$isco_mainjob <= 2659] <- 3
d$class16_r[d$selfem_mainjob == 3 & d$isco_mainjob >= 3000 & d$isco_mainjob <= 9629] <- 3

# Small business owners without employees (4)

d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 1000 & d$isco_mainjob <= 1439] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2163] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2166] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2220 & d$isco_mainjob <= 2240] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2260] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2263 & d$isco_mainjob <= 2269] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2340 & d$isco_mainjob <= 2342] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2353 & d$isco_mainjob <= 2356] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2433 & d$isco_mainjob <= 2434] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2620] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2622] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2635] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob == 2641] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 2650 & d$isco_mainjob <= 2659] <- 4
d$class16_r[d$selfem_mainjob == 2 & d$isco_mainjob >= 3000 & d$isco_mainjob <= 9629] <- 4

# Technical experts (5)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2100 &  d$isco_mainjob <= 2162] <- 5
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2164 &  d$isco_mainjob <= 2165] <- 5
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2500 &  d$isco_mainjob <= 2529] <- 5

# Technicians (6)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3100 &  d$isco_mainjob <= 3155] <- 6
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3210 &  d$isco_mainjob <= 3214] <- 6
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3252] <- 6
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3500 &  d$isco_mainjob <= 3522] <- 6

# Skilled manual (7)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 6000 &  d$isco_mainjob <= 7549] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8310 &  d$isco_mainjob <= 8312] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8330] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8332 &  d$isco_mainjob <= 8340] <- 7
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8342 &  d$isco_mainjob <= 8344] <- 7

# Low-skilled manual (8)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8000 &  d$isco_mainjob <= 8300] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 8320 &  d$isco_mainjob <= 8321] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8341] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8350] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9200 &  d$isco_mainjob <= 9334] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9600 &  d$isco_mainjob <= 9620] <- 8
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9622 &  d$isco_mainjob <= 9629] <- 8

# Higher-grade managers and administrators (9)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1000 &  d$isco_mainjob <= 1300] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1320 &  d$isco_mainjob <= 1349] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2400 &  d$isco_mainjob <= 2432] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2610 &  d$isco_mainjob <= 2619] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2631] <- 9
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 100 &  d$isco_mainjob <= 110] <- 9

# Lower-grade managers and administrators (10)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1310 &  d$isco_mainjob <= 1312] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 1400 &  d$isco_mainjob <= 1439] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2433 &  d$isco_mainjob <= 2434] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3300 &  d$isco_mainjob <= 3339] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3343] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3350 &  d$isco_mainjob <= 3359] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3411] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5221] <- 10
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 200 &  d$isco_mainjob <= 210] <- 10

# Skilled clerks (11)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3340 &  d$isco_mainjob <= 3342] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3344] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4000 &  d$isco_mainjob <= 4131] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4200 &  d$isco_mainjob <= 4221] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4224 &  d$isco_mainjob <= 4413] <- 11
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 4415 &  d$isco_mainjob <= 4419] <- 11

# Unskilled clerks (12)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4132] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4222] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4223] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5230] <- 12
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 9621] <- 12

# Socio-cultural professionals (13)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2200 &  d$isco_mainjob <= 2212] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2250] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2261 &  d$isco_mainjob <= 2262] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2300 &  d$isco_mainjob <= 2330] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2350 &  d$isco_mainjob <= 2352] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2359] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2600] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2621] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2630] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2632 &  d$isco_mainjob <= 2634] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2636 &  d$isco_mainjob <= 2640] <- 13
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2642 &  d$isco_mainjob <= 2643] <- 13

# Socio-cultural semi-professionals (14)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2163] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2166] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2220 &  d$isco_mainjob <= 2240] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2260] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2263 &  d$isco_mainjob <= 2269] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2340 &  d$isco_mainjob <= 2342] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2353 &  d$isco_mainjob <= 2356] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2620] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2622] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2635] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 2641] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 2650 &  d$isco_mainjob <= 2659] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3200] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3220 &  d$isco_mainjob <= 3230] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3250] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3253 &  d$isco_mainjob <= 3257] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3259] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3400 &  d$isco_mainjob <= 3410] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3412 &  d$isco_mainjob <= 3413] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3430 &  d$isco_mainjob <= 3433] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3435] <- 14
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 4414] <- 14

# Skilled service (15)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3240] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3251] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3258] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 3420 &  d$isco_mainjob <= 3423] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 3434] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5000 &  d$isco_mainjob <= 5120] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5140 &  d$isco_mainjob <= 5142] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5163] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5165] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5200] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5220] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5222 &  d$isco_mainjob <= 5223] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5241 &  d$isco_mainjob <= 5242] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5300 &  d$isco_mainjob <= 5321] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5400 &  d$isco_mainjob <= 5413] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5419] <- 15
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8331] <- 15

# Low-skilled service (16)

d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5130 &  d$isco_mainjob <= 5132] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5150 &  d$isco_mainjob <= 5162] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5164] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5169] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5210 &  d$isco_mainjob <= 5212] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5240] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5243 &  d$isco_mainjob <= 5249] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 5322 &  d$isco_mainjob <= 5329] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 5414] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob == 8322] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9100 &  d$isco_mainjob <= 9129] <- 16
d$class16_r[d$selfem_mainjob == 1 & d$isco_mainjob >= 9400 &  d$isco_mainjob <= 9520] <- 16


d$class16_r[d$class16_r == -9] <- NA


d$class8_r <- NA
d$class8_r[d$class16_r <= 2] <- 1
d$class8_r[d$class16_r == 3 | d$class16_r == 4] <- 2
d$class8_r[d$class16_r == 5 | d$class16_r == 6] <- 3
d$class8_r[d$class16_r == 7 | d$class16_r == 8] <- 4
d$class8_r[d$class16_r == 9 | d$class16_r == 10] <- 5
d$class8_r[d$class16_r == 11 | d$class16_r == 12] <- 6
d$class8_r[d$class16_r == 13 | d$class16_r == 14] <- 7
d$class8_r[d$class16_r == 15 | d$class16_r == 16] <- 8

d$class5_r <- NA
d$class5_r[d$class16_r <= 2 | d$class16_r == 5 | d$class16_r == 9 | d$class16_r == 13] <- 1
d$class5_r[d$class16_r == 6 | d$class16_r == 10 | d$class16_r == 14] <- 2
d$class5_r[d$class16_r == 3 | d$class16_r == 4] <- 3
d$class5_r[d$class16_r == 7 | d$class16_r == 11 | d$class16_r == 15] <- 4
d$class5_r[d$class16_r == 8 | d$class16_r == 12 | d$class16_r == 16] <- 5


#######################################################################################
# Partner's Oesch class position
# Recode and create variables used to construct class variable for partners
# Variables used to construct class variable for partners: isco08p, emprelp
#######################################################################################

#### Recode occupation variable (isco88 com 4-digit) for partners

d$isco_partner <- d$isco08p
d$isco_partner[is.na(d$isco_partner)] <- -9

#### Recode employment status for partners

d$selfem_partner <- NA
d$selfem_partner[d$emprelp == 1 | d$emprelp == 6 | d$emprelp == 7 | d$emprelp == 8 | d$emprelp == 9 | is.na(d$emprelp)] <- 1
d$selfem_partner[d$emprelp == 2 | d$emprelp == 3] <- 2

############################################
# Create Oesch class schema for partners
############################################

d$class16_p <- -9

# Large employers (1)

# Self-employed professionals (2)

d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2000 & d$isco_partner <= 2162] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2164 & d$isco_partner <= 2165] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2200 & d$isco_partner <= 2212] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2250] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2261 & d$isco_partner <= 2262] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2300 & d$isco_partner <= 2330] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2350 & d$isco_partner <= 2352] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2359 & d$isco_partner <= 2432] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2500 & d$isco_partner <= 2619] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2621] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2630 & d$isco_partner <= 2634] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2636 & d$isco_partner <= 2640] <- 2
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2642 & d$isco_partner <= 2643] <- 2

# Small business owners with employees (3)

# Small business owners without employees (4)

d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 1000 & d$isco_partner <= 1439] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2163] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2166] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2220 & d$isco_partner <= 2240] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2260] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2263 & d$isco_partner <= 2269] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2340 & d$isco_partner <= 2342] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2353 & d$isco_partner <= 2356] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2433 & d$isco_partner <= 2434] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2620] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2622] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2635] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner == 2641] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 2650 & d$isco_partner <= 2659] <- 4
d$class16_p[d$selfem_partner == 2 & d$isco_partner >= 3000 & d$isco_partner <= 9629] <- 4

# Technical experts (5)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2100 & d$isco_partner <= 2162] <- 5
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2164 & d$isco_partner <= 2165] <- 5
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2500 & d$isco_partner <= 2529] <- 5


# Technicians (6)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3100 & d$isco_partner <= 3155] <- 6
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3210 & d$isco_partner <= 3214] <- 6
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3252] <- 6
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3500 & d$isco_partner <= 3522] <- 6

# Skilled manual (7)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 6000 & d$isco_partner <= 7549] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8310 & d$isco_partner <= 8312] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8330] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8332 & d$isco_partner <= 8340] <- 7
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8342 & d$isco_partner <= 8344] <- 7

# Low-skilled manual (8)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8000 & d$isco_partner <= 8300] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 8320 & d$isco_partner <= 8321] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8341] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8350] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9200 & d$isco_partner <= 9334] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9600 & d$isco_partner <= 9620] <- 8
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9622 & d$isco_partner <= 9629] <- 8

# Higher-grade managers and administrators (9)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1000 & d$isco_partner <= 1300] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1320 & d$isco_partner <= 1349] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2400 & d$isco_partner <= 2432] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2610 & d$isco_partner <= 2619] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2631] <- 9
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 100 & d$isco_partner <= 110] <- 9

# Lower-grade managers and administrators (10)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1310 & d$isco_partner <= 1312] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 1400 & d$isco_partner <= 1439] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2433 & d$isco_partner <= 2434] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3300 & d$isco_partner <= 3339] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3343] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3350 & d$isco_partner <= 3359] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3411] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5221] <- 10
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 200 & d$isco_partner <= 210] <- 10

# Skilled clerks (11)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3340 & d$isco_partner <= 3342] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3344] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4000 & d$isco_partner <= 4131] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4200 & d$isco_partner <= 4221] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4224 & d$isco_partner <= 4413] <- 11
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 4415 & d$isco_partner <= 4419] <- 11

# Unskilled clerks (12)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4132] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4222] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4223] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5230] <- 12
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 9621] <- 12

# Socio-cultural professionals (13)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2200 & d$isco_partner <= 2212] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2250] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2261 & d$isco_partner <= 2262] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2300 & d$isco_partner <= 2330] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2350 & d$isco_partner <= 2352] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2359] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2600] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2621] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2630] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2632 & d$isco_partner <= 2634] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2636 & d$isco_partner <= 2640] <- 13
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2642 & d$isco_partner <= 2643] <- 13

# Socio-cultural semi-professionals (14)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2163] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2166] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2220 & d$isco_partner <= 2240] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2260] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2263 & d$isco_partner <= 2269] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2340 & d$isco_partner <= 2342] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2353 & d$isco_partner <= 2356] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2620] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2622] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2635] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 2641] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 2650 & d$isco_partner <= 2659] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3200] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3220 & d$isco_partner <= 3230] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3250] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3253 & d$isco_partner <= 3257] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3259] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3400 & d$isco_partner <= 3410] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3412 & d$isco_partner <= 3413] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3430 & d$isco_partner <= 3433] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3435] <- 14
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 4414] <- 14

# Skilled service (15)

d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3240] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3251] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3258] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 3420 & d$isco_partner <= 3423] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 3434] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5000 & d$isco_partner <= 5120] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5140 & d$isco_partner <= 5142] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5163] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5165] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5200] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5220] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5222 & d$isco_partner <= 5223] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5241 & d$isco_partner <= 5242] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5300 & d$isco_partner <= 5321] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5400 & d$isco_partner <= 5413] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5419] <- 15
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8331] <- 15

# Low-skilled service (16)

d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5130 & d$isco_partner <= 5132] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5150 & d$isco_partner <= 5162] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5164] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5169] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5210 & d$isco_partner <= 5212] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5240] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5243 & d$isco_partner <= 5249] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 5322 & d$isco_partner <= 5329] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 5414] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner == 8322] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9100 & d$isco_partner <= 9129] <- 16
d$class16_p[d$selfem_partner == 1 & d$isco_partner >= 9400 & d$isco_partner <= 9520] <- 16


d$class8_p <- NA
d$class8_p[d$class16_p <= 2] <- 1
d$class8_p[d$class16_p == 3 | d$class16_p == 4] <- 2
d$class8_p[d$class16_p == 5 | d$class16_p == 6] <- 3
d$class8_p[d$class16_p == 7 | d$class16_p == 8] <- 4
d$class8_p[d$class16_p == 9 | d$class16_p == 10] <- 5
d$class8_p[d$class16_p == 11 | d$class16_p == 12] <- 6
d$class8_p[d$class16_p == 13 | d$class16_p == 14] <- 7
d$class8_p[d$class16_p == 15 | d$class16_p == 16] <- 8


d$class5_p <- NA
d$class5_p[d$class16_p <= 2 | d$class16_p == 5 | d$class16_p == 9 | d$class16_p == 13] <- 1
d$class5_p[d$class16_p == 6 | d$class16_p == 10 | d$class16_p == 14] <- 2
d$class5_p[d$class16_p == 3 | d$class16_p == 4] <- 3
d$class5_p[d$class16_p == 7 | d$class16_p == 11 | d$class16_p == 15] <- 4
d$class5_p[d$class16_p == 8 | d$class16_p == 12 | d$class16_p == 16] <- 5

####################################################################################################
# Final Oesch class position
# Merge two class variables (respondents and partners)
# Assign the partner's Oesch class position when the respondent's Oesch class position is missing:
####################################################################################################

d$class16 <- ifelse(!is.na(d$class16_r), d$class16_r, d$class16_p)


d$class8 <- NA
d$class8[d$class16 <= 2] <- 1
d$class8[d$class16 == 3 | d$class16 == 4] <- 2
d$class8[d$class16 == 5 | d$class16 == 6] <- 3
d$class8[d$class16 == 7 | d$class16 == 8] <- 4
d$class8[d$class16 == 9 | d$class16 == 10] <- 5
d$class8[d$class16 == 11 | d$class16 == 12] <- 6
d$class8[d$class16 == 13 | d$class16 == 14] <- 7
d$class8[d$class16 == 15 | d$class16 == 16] <- 8


d$class5 <- NA
d$class5[d$class16 <= 2 | d$class16 == 5 | d$class16 == 9 | d$class16 == 13] <- 1
d$class5[d$class16 == 6 | d$class16 == 10 | d$class16 == 14] <- 2
d$class5[d$class16 == 3 | d$class16 == 4] <- 3
d$class5[d$class16 == 7 | d$class16 == 11 | d$class16 == 15] <- 4
d$class5[d$class16 == 8 | d$class16 == 12 | d$class16 == 16] <- 5


ess_6_7 <- d
ess_6_7$euftf <- as.double(ess_6_7$euftf)


```

# Merging----

Merge waves 1-5 and waves 6-7.
```{r merge ess}

common_cols <- intersect(colnames(ess_1_5), 
                         colnames(ess_6_7))

ess <- full_join(subset(ess_1_5, select = common_cols),
             subset(ess_6_7, select = common_cols))

ess <- ess %>%
  mutate(class5.f = factor(class5,
                           levels = c(1:5),
                           labels = c("Higher-grade service class",
                            "Lower-grade service class",
                            "Small business owners",
                            "Skilled workers",
                            "Unskilled workers")),
         class8.f = factor(class8,
                          levels = c(1:8),
                          labels = c("Self-employed professionals and large employers",
                            "Small business owners",
                            "Technical (semi-)professionals",
                            "Production workers",
                            "(Associate) managers",
                            "Clerks",
                            "Socio-cultural (semi-)professionals",
                            "Service workers")),
         class16.f = factor(class16,
                            levels = c(1:16),
                            labels = c("Large employers",
                             "Self-employed professionals",
                             "Small business owners with employees",
                             "Small business owners without employees",
                             "Technical experts",
                             "Technicians",
                             "Skilled manual",
                             "Low-skilled manual",
                             "Higher-grade managers and administrators",
                             "Lower-grade managers and administrators",
                             "Skilled clerks",
                             "Unskilled clerks",
                             "Socio-cultural professionals",
                             "Socio-cultural semi-professionals",
                             "Skilled service",
                             "Low-skilled service")))

```

# Recoding----

Recode variables for further use.
```{r recode data}

remove(ess_1, ess_2, ess_3, ess_4, ess_5, ess_6, ess_7)

ess <- recode_missings(ess)

ess$cntry <- as.factor(ess$cntry)

## Demographics

# Cohorts
ess$yrbrn <- ifelse(ess$yrbrn<1900 | ess$yrbrn>1996, NA, ess$yrbrn)

ess <- ess %>%
  mutate(cohort = case_when(
    yrbrn %in% 1900:1945 ~ "1",
    yrbrn %in% 1946:1964 ~ "2",
    yrbrn %in% 1965:1979 ~ "3",
    yrbrn %in% 1980:1996 ~ "4"
  ))

ess$cohort <- as.factor(ess$cohort)

ess$age <- ifelse(ess$agea==999, NA, ess$agea)

ess <- ess %>%
  mutate(gender = case_when(
    gndr == 1 ~ 1,
    gndr == 2 ~ 0))

ess$edulvla <- ifelse(ess$edulvla>5,NA,ess$edulvla)
ess$eisced <- ifelse(ess$eisced>7,NA,ess$eisced)

ess$relig <- ess$rlgdgr

## Attitudes (higher values -> more liberal values)
ess$freehms <- 6-ess$freehms

ess <- ess %>%
  mutate(behave = ipbhprp,
         secure = impsafe,
         safety = ipstrgv,
         tradition = imptrad,
         rules = ipfrule,
         surprise = impdiff,
         adventure = ipadvnt,
         creative = ipcrtiv,
         free = impfree,
         listen = ipudrst) %>%
  mutate(across(c("behave", "secure",
              "safety", "tradition",
              "rules", "surprise",
              "adventure", "creative",
              "free", "listen"), ~ 7 - .x ))

ess$schwartzauth <- scales::rescale(ess$behave + 
                      ess$secure + 
                      ess$safety + 
                      ess$tradition + 
                      ess$rules, to=c(0,100), na.rm=T)

ess$schwartzlib <- scales::rescale(ess$surprise + 
                ess$adventure + 
                ess$creative + 
                ess$free + 
                ess$listen, na.rm=T, to=c(0,100))


# Populist scale (Norris/Inglehart use low levels of trust in parliaments, politicians and parties as an indicator of populism)

## ESS wave 1 does not include the variable "trust in parties."
ess$trust_hlp1 <- as.numeric(ifelse(ess$essround==1, 
                                    ess$trstprl+ess$trstplt, 
                                    "NA"))

ess$trust_hlp2 <- ess$trstplt + ess$trstprl + ess$trstprt

ess$trust_tmp <- ifelse(ess$essround==1, ess$trust_hlp1, ess$trust_hlp2)
ess$trust <- scales::rescale(ess$trust_tmp, na.rm=TRUE, to=c(0,100))
ess$populist <- scales::rescale(ess$trust, na.rm = TRUE, to=c(100,0))

# combined design and population weights
ess$weight <- ess$dweight * ess$pweight

```

# PCA autoritarian and libertarian values----

Principal component analysis: Scales for authoritarian and libertarian values.
```{r}

ess_1_7 <- ess %>%
  select(essround, edition, cntry, idno,
         freehms, imueclt, imwbcnt, euftf, 
         eisced, edulvla, domicil, dweight:pweight, 
         class16:schwartzlib, trust:weight)

schwartz <- ess_1_7 %>%
  select(behave, secure, safety, tradition, rules,
         surprise, adventure, creative, free, listen) 

fit <- principal(schwartz, nfactors=2, rotate="varimax")

fs <- factor.scores(schwartz, fit)
fs <- fs$scores                       
ess_1_7 <- cbind(ess_1_7,fs)

# Save data to an appropriate folder for further use.
saveRDS(ess_1_7, file = (here("processed-data", "ess_1_7.rds")))

```
